﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SimpleRest.Core.Extensions.Media;

namespace SimpleRest.Core.Extensions
{
    public class ExtendedConfigurationOptions
    {
        public bool IsXsdFormatted { get; set; }

        private bool isDefaultXmlMediaTypeFormatted = false;
        public bool IsDefaultXmlMediaTypeFormatted
        {
            get { return isDefaultXmlMediaTypeFormatted; }
            set
            { 
                this.isDefaultXmlMediaTypeFormatted = value;

                this.SetDefaultXmlFormatter();
            }
        }

        private bool isDefaultJsonMediaTypeFormatted = false;
        public bool IsDefaultJsonMediaTypeFormatted
        {
            get { return isDefaultJsonMediaTypeFormatted; }
            set
            {  
                this.isDefaultJsonMediaTypeFormatted = value;

                this.SetDefaultJsonFormatter();
            }
        }

        internal void SetDefaultJsonFormatter()
        {
            if (this.Parent == null || this.Parent.MediaTypeFormatters == null)
                return;
            var formatter = this.Parent.MediaTypeFormatters.FirstOrDefault(d => d is JsonMediaTypeFormatter);
            if (formatter != null)
                (formatter as JsonMediaTypeFormatter).IsMediaFormatted = this.IsDefaultJsonMediaTypeFormatted;
        }
        internal void SetDefaultXmlFormatter()
        {
            if (this.Parent == null || this.Parent.MediaTypeFormatters == null)
                return;
            var formatter = this.Parent.MediaTypeFormatters.FirstOrDefault(d => d is XmlMediaTypeFormatter);
            if (formatter != null)
                (formatter as XmlMediaTypeFormatter).IsMediaFormatted = this.IsDefaultXmlMediaTypeFormatted;
        }

        private readonly ExtendedConfiguration Parent;

        internal ExtendedConfigurationOptions(ExtendedConfiguration parent)
        {
            this.Parent = parent;
        }
    }
}
